#include <bits/stdc++.h>

using namespace std;

const int maxn = 1003;

int n, m;
int f[maxn][maxn];
char a[maxn], b[maxn], ans[maxn];

int main() {
	cin >> a >> b;
	memset(f, 0, sizeof(f));
	f[0][0] = 0;
	n = strlen(a), m = strlen(b);
	for (int i = 1; i <= n; ++ i) {
		for (int j = 1; j <= m; ++ j) {
			if (a[i - 1] == b[j - 1]) {
				f[i][j] = f[i - 1][j - 1] + 1;
			} else {
				f[i][j] = max(f[i - 1][j], f[i][j - 1]);
			}
		}
	}
	cout << f[n][m] << endl;
	ans[f[n][m]] = 0;
	for (int i = n, j = m, k = f[n][m]; i > 0 && j > 0; ) {
		if (a[i - 1] == b[j - 1]) {
			ans[-- k] = a[i - 1];
			-- i, -- j;
		} else if (f[i - 1][j] > f[i][j - 1]) {
			-- i;
		} else {
			-- j;
		}
	}
	cout << ans << endl;
}
